package com.ruoyi.association.service.impl;

import java.util.List;
import java.util.Date;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.association.mapper.SysAssociationMapper;
import com.ruoyi.association.mapper.SysAssociationMemberMapper;
import com.ruoyi.association.domain.SysAssociation;
import com.ruoyi.association.domain.SysAssociationMember;
import com.ruoyi.association.service.ISysAssociationService;

/**
 * 协会Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-01-27
 */
@Service
public class SysAssociationServiceImpl implements ISysAssociationService 
{
    @Autowired
    private SysAssociationMapper sysAssociationMapper;

    @Autowired
    private SysAssociationMemberMapper sysAssociationMemberMapper;

    /**
     * 查询协会
     * 
     * @param associationId 协会主键
     * @return 协会
     */
    @Override
    public SysAssociation selectSysAssociationByAssociationId(Long associationId)
    {
        return sysAssociationMapper.selectSysAssociationByAssociationId(associationId);
    }

    /**
     * 查询协会列表
     * 
     * @param sysAssociation 协会
     * @return 协会
     */
    @Override
    public List<SysAssociation> selectSysAssociationList(SysAssociation sysAssociation)
    {
        return sysAssociationMapper.selectSysAssociationList(sysAssociation);
    }

    /**
     * 新增协会
     * 
     * @param sysAssociation 协会
     * @return 结果
     */
    @Override
    public int insertSysAssociation(SysAssociation sysAssociation)
    {
        sysAssociation.setCreateTime(DateUtils.getNowDate());
        return sysAssociationMapper.insertSysAssociation(sysAssociation);
    }

    /**
     * 修改协会
     * 
     * @param sysAssociation 协会
     * @return 结果
     */
    @Override
    public int updateSysAssociation(SysAssociation sysAssociation)
    {
        sysAssociation.setUpdateTime(DateUtils.getNowDate());
        return sysAssociationMapper.updateSysAssociation(sysAssociation);
    }

    /**
     * 批量删除协会
     * 
     * @param associationIds 需要删除的协会主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysAssociationByAssociationIds(Long[] associationIds)
    {
        // 删除关联的会员信息
        for (Long associationId : associationIds)
        {
            sysAssociationMemberMapper.deleteSysAssociationMemberByAssociationId(associationId);
        }
        return sysAssociationMapper.deleteSysAssociationByAssociationIds(associationIds);
    }

    /**
     * 删除协会信息
     * 
     * @param associationId 协会主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysAssociationByAssociationId(Long associationId)
    {
        // 删除关联的会员信息
        sysAssociationMemberMapper.deleteSysAssociationMemberByAssociationId(associationId);
        return sysAssociationMapper.deleteSysAssociationByAssociationId(associationId);
    }

    /**
     * 校验协会编码是否唯一
     * 
     * @param sysAssociation 协会信息
     * @return 结果
     */
    @Override
    public String checkAssociationCodeUnique(SysAssociation sysAssociation)
    {
        Long associationId = StringUtils.isNull(sysAssociation.getAssociationId()) ? -1L : sysAssociation.getAssociationId();
        SysAssociation info = sysAssociationMapper.checkAssociationCodeUnique(sysAssociation.getAssociationCode());
        if (StringUtils.isNotNull(info) && info.getAssociationId().longValue() != associationId.longValue())
        {
            return "1";
        }
        return "0";
    }

    /**
     * 根据协会ID查询关联会员列表
     * 
     * @param associationId 协会ID
     * @return 会员列表
     */
    @Override
    public List<SysAssociationMember> selectMembersByAssociationId(Long associationId)
    {
        return sysAssociationMemberMapper.selectMembersByAssociationId(associationId);
    }

    /**
     * 根据会员ID查询关联协会列表
     * 
     * @param membersId 会员ID
     * @return 协会列表
     */
    @Override
    public List<SysAssociationMember> selectAssociationsByMembersId(Long membersId)
    {
        return sysAssociationMemberMapper.selectAssociationsByMembersId(membersId);
    }

    /**
     * 添加会员到协会
     * 
     * @param sysAssociationMember 协会会员关联
     * @return 结果
     */
    @Override
    @Transactional
    public int addMemberToAssociation(SysAssociationMember sysAssociationMember)
    {
        sysAssociationMember.setJoinTime(DateUtils.getNowDate());
        sysAssociationMember.setStatus("0");
        int result = sysAssociationMemberMapper.insertSysAssociationMember(sysAssociationMember);
        // 更新协会会员数量
        updateMemberCount(sysAssociationMember.getAssociationId());
        return result;
    }

    /**
     * 从协会移除会员
     * 
     * @param id 关联ID
     * @return 结果
     */
    @Override
    @Transactional
    public int removeMemberFromAssociation(Long id)
    {
        SysAssociationMember member = sysAssociationMemberMapper.selectSysAssociationMemberById(id);
        int result = sysAssociationMemberMapper.deleteSysAssociationMemberById(id);
        // 更新协会会员数量
        if (member != null)
        {
            updateMemberCount(member.getAssociationId());
        }
        return result;
    }

    /**
     * 更新协会会员数量
     * 
     * @param associationId 协会ID
     * @return 结果
     */
    @Override
    public int updateMemberCount(Long associationId)
    {
        return sysAssociationMapper.updateMemberCount(associationId);
    }
}
